define(function(require, exports){
	var $		= require("jquery");
	var pubjs	= require('pubjs');
	var popwin	= require('@base/dialog').base;
	var util = require('util');
	var grid     = require('@base/grid');

	var selectAdvertiser = popwin.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'title': LANG('广告主选择'),
				'width': 800,
				//'showFoot':0,
				'size': 'm',
				'silence': false
			});

			this.$channels = pubjs.config('channelMap');
			this.$data = {};
			this.Super('init', arguments);
		},
		afterBuild:function(){
			this.Super('afterBuild',arguments);
			var el = this.getDOM('body');
			el.append($([
				// '<div class="select" pub-name="grid"></div>'
				'<span pub-name="selectAdvertiser" pub-mod="@base/grid.baseNoDate" />'
			].join('')));

			var config = {
				selectAdvertiser: {
					'hasSearch': true,
					'hasSubGrid': false,
					'hasExport': false,
					'hasAmount': false,
					'hasTab': false,
					'hasSelect': true,
					'reqType': 'websocket',
					'url': 'qualification/list',
					'cols': [
						{name:'_id',text:"ID", width: 60, align: 'center'},
						{name:'Name',text:LANG("公司名称"), align: 'left', width: 250, render: this.renderCName}
					],
					'pager': {
						'size': 8,
						'bounds': 4,
						'showSizeTypes': false,
						'showJumper': false
					},
					'list':{
						'rowSelect': true,
						'scroll_type': 'row',
						'scroll_size': 5
					},
					'param': {
						'order': 'CreateTime|-1'
					},
				}
			};

			this.buildFromTemplate(config);
		},
		afterBuildTemplate: function(){

		},
		renderName: function(i, val, data, con) {
			val = LANG(val) || '';

			var html;

			if (pubjs.checkRight('qualification.update')) {
				html = $('<a class="db text-truncate" title="' + val + '">' + val + '</a>').attr({
					'href': '#admin/aptitude/edit/' + row._id,
					'target': '_blank'
				});
			} else {
				html = $('<div class="text-truncate" title="' + val + '">' + val + '</div>');
			}

			return html;
		},
		// 选中消息
		onChangeSelect: function(ev) {
			var c = this.getConfig();
			var data = {};
			util.each(ev.param.data, function(item, idx){
				data[item._id] = {
					'id': item._id,
					'name': item[c.nameField || 'Name']
				};
			});
			this.$data = data;
			return false;
		},
		getValue: function() {
			return this.$data;
		},
		onInputClick: function(ev){
			switch (ev.name){
				case 'buttonCancel': 
					return this.hide();
				case 'buttonOk':
					this.fire('selectFinish',this.$data);
					return this.hide();
			}
		},
		onRmItem: function(ev){
			this.$.selectAdvertiser.$.list.removeSelectRowId(ev.param['id']);
			return false;
		},
		onSetItem: function(ev){
			this.$.selectAdvertiser.$.list.setSelectRowIds(ev.param);
			return false;
		},
		setParam: function(param){
			this.$.selectAdvertiser.setParam(param);
			return this;
		},
		load: function(){
			this.$.selectAdvertiser.reload();
			return this;
		}
	});

	exports.selectPopwin = selectAdvertiser;

});